﻿using DongFangGuoKai.TaskPlatform.Commn.ApiModule;
using DongFangGuoKai.TaskPlatform.Commn.ApiModule.Dto;
using DongFangGuoKai.TaskPlatform.Commn.Attributes;
using DongFangGuoKai.TaskPlatform.Commn.ConfigureOptions;
using DongFangGuoKai.TaskPlatform.Commn.LogModule;
using DongFangGuoKai.TaskPlatform.Job.InventoryReturn.Model;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Quartz;
using RestSharp;
using SqlSugar;

namespace DongFangGuoKai.TaskPlatform.Job.InventoryReturn
{
    [Job("盘点任务", "盘点回传", "盘点回传任务2分钟执行一次")]
    public class InventoryReturnJob : IJob
    {
        public IApiClient _apiClient { get; set; }
        public IOptions<UrlConfigOption> _urlOption { get; set; }
        public LogHelper<InventoryReturnJob> _log { get; set; }

        public SyncIDType _syncID = SyncIDType.InventoryOut;

        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                UrlConfigOption? urlConfigOption = _urlOption.Value;
                string url = Path.Combine(urlConfigOption.WMSUrl, JobHelper.GetWMSSuffixUrl(_syncID));
                var result = _apiClient.Get<ApiDataResult<JToken>>(url);

                if (result.Code != 200)
                {
                    _log.LogError(url, null, result.Msg, result.Code.ToString());
                    await Task.CompletedTask;
                    return;
                }
                if (!result.Data.Any())
                {
                    await Task.CompletedTask;
                    return;
                }
                var data = result.Data.ToObject<InventoryReturnModel>();
                var inventoryReturnDto = new InventoryReturnDto()
                {
                    PlanId = data.PlanId,
                    CheckResult = data.CheckResult,
                    SupervisorId = data.SupervisorId,
                    UserId = data.UserId
                };
                if (data.Items != null && data.Items.Count > 0)
                {
                    inventoryReturnDto.Items = new();
                    foreach (var item in data.Items)
                    {
                        inventoryReturnDto.Items.Add(new InventoryReturnDetailDto()
                        {
                            StockId = item.StockId,
                            CheckAmount = item.CheckAmount,
                            PeriodSurplus = item.PeriodSurplus,
                            ProfitLostAmount = item.ProfitLostAmount,
                            QualityStatus = item.QualityStatus
                        });
                    }
                }

                string postUrl = Path.Combine(urlConfigOption.HaiNingUrl, JobHelper.GetHHSuffixUrl(_syncID));
                var res = _apiClient.Post<ApiResultData>(postUrl, inventoryReturnDto);
                if (res.Code != "200")
                {
                    _log.LogError(postUrl, JsonConvert.SerializeObject(data), res.Msg);
                    await Task.CompletedTask;
                    return;
                }
                string putUrl = Path.Combine(urlConfigOption.WMSPDAUrl, "InventoryOrder/UpdateStocktakingOrderStatus");

                var para = new QueryParameter("OrderNum", data.PlanNumber);
                var putRes = _apiClient.Put<ApiDataResult<object>>(putUrl, new Parameter[1]
                        {
                       para
                        });
                if (putRes.Code != 200)
                {
                    _log.LogError(putUrl, "OrderSn:" + data.PlanNumber, "盘点状态回传失败，详情:" + putRes.Msg, putRes.Code.ToString());
                    await Task.CompletedTask;
                    return;
                }
                _log.LogSuc(postUrl, JsonConvert.SerializeObject(inventoryReturnDto));



            }
            catch (Exception ex)
            {
                _log.LogCtmError(ex.Message);

            }



            await Task.CompletedTask;
        }
    }
}
